/**
 * 
 */
package systole.view.crud.medic;

import java.awt.Frame;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;
import javax.swing.JDialog;
import systole.domain.persons.Medic;
import systole.domain.persons.identityCard.IdentityCard;
import systole.domain.persons.identityCard.IdentityCardType;
import systole.exceptions.ExceptionDAO;
import systole.utils.ImageUtils;
import systole.view.crud.controller.ControllerEntityEdition;
import systole.view.messages.EntityDescriptions;

/**
 * @author jmj
 *
 */
public class ControllerMedicEdition extends ControllerEntityEdition {

    /**
     * 
     */
    public ControllerMedicEdition() {
        super();
        this.entityName = EntityDescriptions.MEDIC;
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityFromForm()
     */
    @Override
    protected void loadEntityFromForm() {
        Medic medicToLoad = (Medic) this.curretnEntity;
        JDialogMedic formToLoad = (JDialogMedic) this.editForm;

        medicToLoad.setSurname(formToLoad.getjEdtSurname().getText());
        medicToLoad.setName(formToLoad.getjEdtName().getText());

        if (medicToLoad.getIdentityCard() == null) {
            medicToLoad.setIdentityCard(new IdentityCard());
        }

        medicToLoad.getIdentityCard().setCardType(
                (IdentityCardType) formToLoad.getjCbmIdentityType().getSelectedItem());
        try {
            medicToLoad.getIdentityCard().setNumber(
                    Integer.parseInt(formToLoad.getjEdtIdentityNumber().getText()));
        } catch (NumberFormatException e) {
            medicToLoad.getIdentityCard().setCardType(null);
        }

        medicToLoad.setSex((String) formToLoad.getjCbmSex().getSelectedItem());
        formToLoad.getjCbmSex().setSelectedItem(medicToLoad.getSex());
        medicToLoad.setCellphone(formToLoad.getjEdtCellphone().getText());
        medicToLoad.setEmail(formToLoad.getjEdtMail().getText());
        medicToLoad.setTelephone(formToLoad.getjEdtTelephone().getText());
        medicToLoad.setBirthdate(formToLoad.getjXDatePickerBirthdate().getDate());
        medicToLoad.setSpecialty(formToLoad.getjTxtSpeciality().getText());
        medicToLoad.setLicense(formToLoad.getjTxtLicense().getText());
        medicToLoad.setLastUpdate(Calendar.getInstance());
    }

    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#loadEntityOnForm()
     */
    @Override
    protected void loadEntityOnForm() {
        JDialogMedic formToLoad = (JDialogMedic) this.editForm;
        Medic currentMedic = (Medic) this.curretnEntity;
        formToLoad.getjEdtSurname().setText(currentMedic.getSurname());
        formToLoad.getjEdtName().setText(currentMedic.getName());
        if (currentMedic.getIdentityCard() != null) {
            formToLoad.getjCbmIdentityType().setSelectedItem(
                    currentMedic.getIdentityCard().getCardType());
            formToLoad.getjEdtIdentityNumber().setText(
                    currentMedic.getIdentityCard().getNumber().toString());
        }
        formToLoad.getjCbmSex().setSelectedItem(currentMedic.getSex());
        formToLoad.getjEdtCellphone().setText(currentMedic.getCellphone());
        formToLoad.getjEdtMail().setText(currentMedic.getEmail());
        formToLoad.getjEdtTelephone().setText(currentMedic.getTelephone());
        formToLoad.getjXDatePickerBirthdate().setDate(
                currentMedic.getBirthdate());
        formToLoad.getjTxtSpeciality().setText(currentMedic.getSpecialty());
        formToLoad.getjTxtLicense().setText(currentMedic.getLicense());
    }


    /* (non-Javadoc)
     * @see systole.view.crud.controller.ControllerEntityEdition#valid()
     */
    @Override
    protected String valid() {
        JDialogMedic formToValidate = (JDialogMedic) this.editForm;
        if ((formToValidate.getjEdtName().getText() == null) || (formToValidate.getjEdtName().getText().isEmpty())) {
            return "Debe ingresar el nombre";
        }
        if ((formToValidate.getjEdtSurname().getText() == null) || (formToValidate.getjEdtSurname().getText().isEmpty())) {
            return "Debe ingresar el apellido";
        }
        return null;
    }

    @Override
    protected void save() throws ExceptionDAO {
            if (this.isEditing()) {
                this.facadeDB.getMedicBroker().update((Medic) this.curretnEntity);
            } else {
                this.facadeDB.getMedicBroker().insert((Medic) this.curretnEntity);
            }
    }

    @Override
    protected void loadIconOnForm() {
        this.editForm.setIconImage(ImageUtils.buildImage("resources/icons/medic/medics16.png"));
    }

    @Override
    protected Object createEntity() {
        return new Medic();
    }

    @Override
    protected JDialog createEditionForm(Frame parent) {
        return new JDialogMedic(parent, this);
    }

    @Override
    protected void initFormsControls() throws ExceptionDAO {
        JDialogMedic medicForm = (JDialogMedic) this.editForm;

        List<IdentityCardType> identityCardTypes = this.facadeDB.getIdentityCardBroker().getAllIdentityCardTypes();
        medicForm.getjCbmIdentityType().removeAllItems();

        Iterator<IdentityCardType> itCardType = identityCardTypes.iterator();
        while (itCardType.hasNext()) {
            medicForm.getjCbmIdentityType().addItem(itCardType.next());
        }
        this.editForm = medicForm;
    }
}
